import {createRouter, createWebHistory} from 'vue-router'
import LoginCom from '../components/Login.vue'
import Home from '../components/home.vue'
import Welcome from '../components/Welcome.vue'
import Users from "@/components/user/Users.vue";
import Rights from '../components/power/Rights.vue'
import  Roles from '../components/power/Roles.vue'
import Goods from '../components/ware/Goods.vue'
import Orders from '../components/orders/Orders.vue'
import Reports from "@/components/reports/Reports.vue";
import Categories from "@/components/ware/Categories.vue";
import Params from "@/components/ware/Params.vue";
const routes = [
    {
        path: '/',
        redirect: '/login'
    },
    {
        path: '/login',
        name: 'login',
        component: LoginCom
    },
    {
        path: '/home',
        name: 'Home',
        component: Home,
        redirect: '/welcome',
        children: [{
            path: '/welcome',
            component: Welcome,
        },
            {
                path: '/users',
                component: Users
            },
            {
                path:'/rights',
                component: Rights
            },
            {
                path: '/roles',
                component: Roles
            },
            {
                path: '/goods',
                component: Goods
            },
            {
                path: '/orders',
                component: Orders
            },
            {
                path: '/reports',
                component: Reports
            },
            {
                path: '/categories',
                component: Categories
            },
            {
                path: '/params',
                component: Params
            }


        ]
    }
]


const router = createRouter({
    history: createWebHistory(process.env.BASE_URL),
    routes
})

// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
    // to将要访问的路径
    // from代表路径跳转而来
    // next是一个函数,表示放行
    // next()放行
    if (to.path == '/login') return next()
    // 获取token
    const tokenStr = window.sessionStorage.getItem('token')
    if (!tokenStr) return next('/login')
    next()
})
export default router
